public class ListTable {

    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode result;
        int c=0;
        int temp;
        temp=l1.val+l2.val;
        if(temp>=10){
            c=1;
            temp=temp%10;
        }
        result=new ListNode(temp);
        ListNode p=result;
        while (l1.next!=null||l2.next!=null){
            int a1,a2;
            if(l1.next==null){
                a1=0;
                l2=l2.next;
                a2=l2.val;
            }else if(l2.next==null){
                a2=0;
                l1=l1.next;
                a1=l1.val;
            }else {
                l1=l1.next;
                l2=l2.next;
                a1=l1.val;
                a2=l2.val;

            }
            int t=a1+a2+c;
            if(t>=10){
                c=1;
                t=t%10;
            }else {
                c=0;
            }
            ListNode next=new ListNode(t);
            p.next=next;
            p=p.next;
        }
        if(c==1){
            p.next=new ListNode(1);
        }
        return result;
    }

    public ListNode oddEvenList(ListNode head) {
        ListNode p=head;
        int len=0;
        while (p!=null){
            p=p.next;
            len++;
        }
        if(len<3)return head;
        else {
            p=head;
            ListNode q=head;
            for (int i=0;i<len/2;i++){
                q=head.next;
                ListNode r=head;
                for (int j=0;j<2*i+1;j++){
                    q=q.next;
                    r=r.next;
                    if(q==null)return head;
                }
                r.next=q.next;
                q.next=p.next;
                p.next=q;
                p=p.next;
            }
        }
        return head;
    }

    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode t1=headA;
        ListNode t2=headB;
        if(t1==null||t2==null) return null;
        int len1=1;
        while (t1.next!=null){
            t1=t1.next;
            len1++;
        }
        int len2=1;
        while (t2.next!=null){
            t2=t2.next;
            len2++;
        }
        ListNode tA=t1,tB=t2;
        if(tA!=tB) return null;
        else {
            t1=headA;t2=headB;
            if(len1>len2){
                for (int i=0;i<len1-len2;i++){
                    t1=t1.next;
                }
            }else {
                for (int j=0;j<len2-len1;j++){
                    t2=t2.next;
                }
            }
            while (t1!=t2){
                t1= t1.next;
                t2=t2.next;
            }
            return t1;
        }
    }

}
